﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.Helper;

namespace Zwms.Client.ManageWms
{
    public partial class CountBillResultWnd : UserControl
    {
        private BilCountBill _baseInfo;
        private IList<BilCountBillDetail> _detailList;
        private IList<BilCountBillDetail> _subDetailList;
        private DataTable _recordTable;
        private PagerIndex _pagerIndex;

        public CountBillResultWnd()
        {
            InitializeComponent();
            InitBtnImage();
            this._baseInfo = new BilCountBill();
            this._detailList = new List<BilCountBillDetail>();
            this._subDetailList = new List<BilCountBillDetail>();
            this._pagerIndex = new PagerIndex(1, CommonDefine.iPageSize);
        }

        private void InitBtnImage()
        {
            this.btnOpen.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnSubmit.Image = global::Zwms.Client.Properties.Resources.ok;
        }

        private void ResetDataPager()
        {
            this.dataPager.PageCurrent = 1;
            this.dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void ClearUI()
        {
            tbCountBillCode.Text = string.Empty;
            tbCountDate.Text = string.Empty;
            tbOperatorCode.Text = string.Empty;
            tbOperatorName.Text = string.Empty;
            this.bilCountBillDetailBindingSource.DataSource = null;
        }

        private void LoadSubList(int startIndex, int endIndex)
        {
            this._subDetailList.Clear();
            if (startIndex >= this._detailList.Count)
                return;
            if (endIndex >= this._detailList.Count)
                endIndex = this._detailList.Count - 1;
            BilCountBillDetail item = new BilCountBillDetail();
            for (int i = startIndex; i <= endIndex; i++)
            {
                item = this._detailList.ElementAt(i);
                this._subDetailList.Add(item);
            }

        }

        private void LoadDetailToDgv(string strFileName)
        {
            if (string.IsNullOrWhiteSpace(strFileName))
            {
                return;
            }
            try
            {
                SqliteHelper helper = new SqliteHelper(strFileName);
                _recordTable = helper.ExecuteQuery("select * from countbill");
                if (_recordTable == null)
                {
                    return;
                }
                
                this._detailList.Clear();
                foreach (DataRow row in _recordTable.Rows)
                {
                    BilCountBillDetail item = new BilCountBillDetail();
                    item.CountBillCode = row[0].ToString();
                    item.StockCode = row[1].ToString();
                    item.GoodsCode = row[2].ToString();
                    item.GoodsName = row[3].ToString();
                    item.GoodsBarCode = row[4].ToString();
                    item.SingleGoodsCode = row[5].ToString();
                    item.Spec = Convert.ToInt32(row[6]);
                    item.IBDate = row[7].ToString();
                    item.LocationCode = row[8].ToString();
                    item.PkgQty = Convert.ToInt32(row[9]);
                    item.PieceQty = Convert.ToInt32(row[10]);
                    item.CountedPkgQty = Convert.ToInt32(row[11]);
                    item.CountedPieceQty = Convert.ToInt32(row[12]);
                    this._detailList.Add(item);
                    
                }
                this.LoadSubList(0, CommonDefine.iPageSize - 1);
                this.bilCountBillDetailBindingSource.DataSource = null;
                this.bilCountBillDetailBindingSource.DataSource = this._subDetailList;
                dataPager.RecordCount = this._detailList.Count;
                this.Cursor = Cursors.Default;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void LoadBaseInfo(string strFileName)
        {
            if (string.IsNullOrWhiteSpace(strFileName))
            {
                return;
            }
            try
            {
                SqliteHelper helper = new SqliteHelper(strFileName);
                DataRow row = helper.ExecuteRow("select * from baseinfo");
                if (row == null)
                {
                    return;
                }
                this._baseInfo.Id = row[0].ToString();
                this._baseInfo.CountDate = row[1].ToString();
                this._baseInfo.CountOperatorCode = row[2].ToString();
                this._baseInfo.CountOperatorName = row[3].ToString();
                this.tbCountBillCode.Text = this._baseInfo.Id;
                this.tbCountDate.Text = this._baseInfo.CountDate;
                this.tbOperatorCode.Text = this._baseInfo.CountOperatorCode;
                this.tbOperatorName.Text = this._baseInfo.CountOperatorName;
            }
            catch
            {
                return;
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Text Files (*.db3)|*.db3";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string filename = dlg.FileName;
                this.LoadBaseInfo(filename);
                this.LoadDetailToDgv(filename);
            }
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbCountBillCode.Text))
            {
                MessageBox.Show("没有打开任何盘点结果文件！");
                return;
            }
            try
            {
                this.Cursor = Cursors.WaitCursor;
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                BilCountBill updCountBill = new BilCountBill();
                updCountBill.Id = this._baseInfo.Id;
                updCountBill = client.QueryCountBill(updCountBill);
                if (updCountBill != null)
                {
                    updCountBill.CountDate = this._baseInfo.CountDate;
                    updCountBill.CountOperatorCode = this._baseInfo.CountOperatorCode;
                    updCountBill.CountOperatorName = this._baseInfo.CountOperatorName;
                    updCountBill.BillStatus = "2";
                    client.UpdateCountBill(updCountBill);
                }

                BilCountBillDetail updDtl = new BilCountBillDetail();
                foreach (BilCountBillDetail Item in this._detailList)
                {
                    updDtl.CountBillCode = Item.CountBillCode;
                    updDtl.StockCode = Item.StockCode;
                    updDtl = client.QueryCountBillDetail(updDtl);
                    if (updDtl != null)
                    {
                        updDtl.CountedPkgQty = Item.CountedPkgQty;
                        updDtl.CountedPieceQty = Item.CountedPieceQty;
                        client.UpdateCountBillDetail(updDtl);
                    }
                }
                this.Cursor = Cursors.Default;
                MessageBox.Show("盘点数据入库成功！");
                InfoSys.SysLogWriter.WriteOptLog("盘点数据入库", "CountBillDataSubmit", "入库");
            }
            catch
            {
                this.Cursor = Cursors.Default;
            }
            

        }
    }
}
